package com.seveneight.baijiesteward.ui.main.activity;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.main.presenter.LoginPresenter;
import com.seveneight.baijiesteward.ui.user.model.BranchGradeModel;
import com.seveneight.baijiesteward.utils.StringUtils;
import com.seveneight.baijiesteward.views.ProgressDialog;

import butterknife.BindView;

public class UpdateCodeActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.layout_bar)
    RelativeLayout layout_bar;
    @BindView(R.id.update_code)
    EditText update_code;
    ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_update_code);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_update_code);
    }

    @Override
    protected void initView() {
        findViewById(R.id.submit).setOnClickListener(this);
        dialog=new ProgressDialog();
    }

    @Override
    protected void initData() {
        layout_bar.setBackgroundColor(Color.parseColor("#4863C3"));
        tv_title.setText("立即升级");
    }

    @Override
    protected void setListener() {
        iv_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.submit:
                if (StringUtils.isEmpty(update_code.getText().toString())){
                    Toast.makeText(getContext(), "请输入升级码", Toast.LENGTH_SHORT).show();
                }else {
                    submit();
                }
                break;
        }
    }
    public void submit(){
        if (null!=dialog)
            dialog.show(getContext(),"请求中...");
        new BranchGradeModel().getBankList(getContext(), jsonFormat.GradeCode(update_code.getText().toString()), new MyCallBack<AnyBean>() {
            @Override
            public void onSuccess(AnyBean anyBean) {
                if (anyBean.getRedeemActivieResult()==100){
                    UpdateCodeActivity.this.finish();
                    new LoginPresenter(null).getUserInfo("",getContext(),jsonFormat.commonParam());
                }else {
                    Toast.makeText(getContext(), ""+anyBean.getMessage(), Toast.LENGTH_SHORT).show();
                }
                if (null!=dialog)
                    dialog.cancle();
            }

            @Override
            public void onFailed() {
                if (null!=dialog)
                    dialog.cancle();
            }
        });
    }
}
